package com.travel.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.app.utils.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.core.aspect.Log;
import com.travel.entity.Hotel;
import com.travel.service.HotelService;
import com.travel.utils.FileUploadUtil;

@Controller
@RequestMapping("/Hotel")
public class HotelController {

	@Autowired
	HotelService hotelService;
	
	@Value("${uploadPath}")
	private String uploadPath;
	
	@Log("查询旅行管理列表")
	@RequiresPermissions("HotelList")
    @RequestMapping(value = "/list.do", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> list(ServletRequest request, ServletResponse response, Page<Hotel> page, Hotel view) {
        Map<String, Object> map = new HashMap<>();
        page.setDescs("id");
        List<Hotel> list = hotelService.getHotels(page, view);
        map.put("records", list);
        map.put("total", page.getTotal());
        return map;
    }
	
	@Log("进入旅行管理")
    @RequiresPermissions("Hotel")
    @RequestMapping("/golist.do")
    public String tolist() {
        return "module/Hotel/list";
    }
	
	@Log("旅行管理查询")
    @RequiresPermissions("EditHotel")
    @RequestMapping("/goEdit.do")
    public ModelAndView goEdit(Hotel entity) {
        Map<String, Object> map = new HashMap<>();
        if (entity.getId() != null) {
            entity = this.hotelService.selectById(entity.getId());
        }
        map.put("entity", entity);
        return new ModelAndView("module/Hotel/edit", map);
    }

	@Log("旅行管理保存")
    @RequiresPermissions("EditHotel")
    @RequestMapping(value = "/save.do", method = RequestMethod.POST)
    @ResponseBody
    public Result save(Hotel entity) {
        boolean n = this.hotelService.insertOrUpdate(entity);
        return Result.returnResult(n);
    }

	@Log("旅行管理删除")
    @RequiresPermissions("DelHotel")
    @RequestMapping(value = "/del.do", method = RequestMethod.POST)
    @ResponseBody
    public Result del(Hotel entity) {
        boolean n = this.hotelService.deleteById(entity.getId());
        return Result.returnResult(n);
    }

	@Log("旅行管理修改")
    @RequiresPermissions("UpdateHotel")
    @RequestMapping(value = "/update.do", method = RequestMethod.POST)
    @ResponseBody
    public Result update(Hotel entity) {
        boolean n = this.hotelService.updateById(entity);
        return Result.returnResult(n);
    }

	@Log("旅行管理上传图片")
    @ResponseBody
	@RequestMapping(value = "/upload.do", method = RequestMethod.POST)
    public String upload(@RequestParam("file") MultipartFile file,HttpServletRequest request, HttpServletResponse response) {
    	String fileName = file.getOriginalFilename();
        if(fileName.indexOf("\\") != -1){
            fileName = fileName.substring(fileName.lastIndexOf("\\"));
        }
        String dpath = new SimpleDateFormat("/yyyy/MM/dd/").format(new Date());
        String filePath = uploadPath+dpath;
        String img=FileUploadUtil.getFileName(fileName);
        File targetFile = new File(filePath);
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(filePath+img);
            out.write(file.getBytes());
            out.flush();
            out.close();
            return uploadPath+dpath+img;
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
		
    }
}
